home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS02.ADF / Emacs / fileio.c < prev    next >
C/C++ Source or Header  |  1989-05-30  |  3KB  |  124 lines

  1. /* fileio.c */
  2.  
  3.  
  4. /*
  5.  * The routines in this file read and write ASCII files from the disk. All of
  6.  * the knowledge about files are here. A better message writing scheme should
  7.  * be used.
  8.  */
  9. #include        <stdio.h>
  10. #include        "ed.h"
  11.  
  12. FILE    *ffp;                           /* File pointer, all functions. */
  13.  
  14. /*
  15.  * Open a file for reading.
  16.  */
  17. ffropen(fn)
  18. char    *fn;
  19. {
  20.         if ((ffp=fopen(fn, "r")) == NULL)
  21.                 return (FIOFNF);
  22.         return (FIOSUC);
  23. }
  24.  
  25. /*
  26.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  27.  * (cannot create).
  28.  */
  29. ffwopen(fn)
  30. char    *fn;
  31. {
  32. #if     VMS
  33.         register int    fd;
  34.  
  35.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  36.         || (ffp=fdopen(fd, "w")) == NULL) {
  37. #else
  38.         if ((ffp=fopen(fn, "w")) == NULL) {
  39. #endif
  40.                 mlwrite("Cannot open file for writing");
  41.                 return (FIOERR);
  42.         }
  43.         return (FIOSUC);
  44. }
  45.  
  46. /*
  47.  * Close a file. Should look at the status in all systems.
  48.  */
  49. ffclose()
  50. {
  51. #if     V7
  52.         if (fclose(ffp) != FALSE) {
  53.                 mlwrite("Error closing file");
  54.                 return(FIOERR);
  55.         }
  56.         return(FIOSUC);
  57. #endif
  58.         fclose(ffp);
  59.         return (FIOSUC);
  60. }
  61.  
  62. /*
  63.  * Write a line to the already opened file. The "buf" points to the buffer,
  64.  * and the "nbuf" is its length, less the free newline. Return the status.
  65.  * Check only at the newline.
  66.  */
  67. ffputline(buf, nbuf)
  68. char    buf[];
  69. {
  70.         register int    i;
  71.  
  72.         for (i = 0; i < nbuf; ++i)
  73.                 fputc(buf[i]&0xFF, ffp);
  74.  
  75.         fputc('\n', ffp);
  76.  
  77.         if (ferror(ffp)) {
  78.                 mlwrite("Write I/O error");
  79.                 return (FIOERR);
  80.         }
  81.  
  82.         return (FIOSUC);
  83. }
  84.  
  85. /*
  86.  * Read a line from a file, and store the bytes in the supplied buffer. The
  87.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  88.  * at the end of the file that don't have a newline present. Check for I/O
  89.  * errors too. Return status.
  90.  */
  91. ffgetline(buf, nbuf)
  92. register char   buf[];
  93. {
  94.         register int    c;
  95.         register int    i;
  96.  
  97.         i = 0;
  98.  
  99.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  100.                 if (i >= nbuf-1) {
  101.                         mlwrite("File has long line");
  102.                         return (FIOERR);
  103.                 }
  104.                 buf[i++] = c;
  105.         }
  106.  
  107.         if (c == EOF) {
  108.                 if (ferror(ffp)) {
  109.                         mlwrite("File read error");
  110.                         return (FIOERR);
  111.                 }
  112.  
  113.                 if (i != 0) {
  114.                         mlwrite("File has funny line at EOF");
  115.                         return (FIOERR);
  116.                 }
  117.                 return (FIOEOF);
  118.         }
  119.  
  120.         buf[i] = 0;
  121.         return (FIOSUC);
  122. }
  123.  
  124.